const Realm = require('realm');

// Define your models and their properties
const ModalName = {
	car: 'Car',
	person: 'Person',
};

const CarSchema = {
	name: ModalName.car,
	properties: {
		make:  'string',
		model: 'string',
		miles: {type: 'int', default: 0},
	}
};
const PersonSchema = {
	name: ModalName.person,
	properties: {
		name:     'string',
		birthday: 'date',
		cars:     'Car[]',
		picture:  'data?' // optional property
	}
};

const config = {schema: [CarSchema, PersonSchema], path: './carSchema.realm'};

const operateSync = () => {

	const myRealm = new Realm(config);

	// for (let i = 1; i < 20; i += 1) {
	// 	myRealm.write(() => {
	// 		myRealm.create(ModalName.car, {
	// 			make: 'CarMaker,'+i,
	// 			model: 'car model',
	// 			miles: i,
	// 		});
	// 	});
	// }
	const cars = myRealm.objects(ModalName.car);
	console.log(cars.length);
	myRealm.close();
	
};

const operateAsync = () => {
	Realm.open({schema: [CarSchema, PersonSchema], path: './carSchema.realm'})
		.then(realm => {
			const cars = realm.objects('Car').filtered('miles > 10');
			console.log(cars.length);
		}).catch(error => {
			console.log(error);
		});
};


// operateAsync();
operateSync();
